<?php
/**
 * Answer Service
 * User: zng
 * Date: 2019/11/4
 * Time: 14:34
 */
namespace app\admin\service\questions;

use app\admin\model\questions\ExaminationType as ExaminationTypeModels;

Class ExaminationType {

    /**
     * 首页列表
     * @param array $get 筛选条件
     * @return \think\Paginator
     */
    public function indexPaginate( array $get=[] )
    {
        $map = [];

        if ( get($get, 'title') ) {
            $map[] = ['title', 'LIKE', '%'.$get['title'].'%'];
        }
        $config = config('field.examination_type');
        if ( in_array(get($get, 'type'), array_keys($config['type']) ) ) {
            $map[] = [ 'type', '=', $get['type'] ];
        }
        $pageSize = get($get,'pageSize')>0?$get['pageSize']:20;

        return ExaminationTypeModels::where($map)
            ->order('id', 'desc')
            ->paginate($pageSize);
    }

    /**
     * 新增考试类型信息
     * @param array $data 数据包
     * @return boolean|integer
     */
    public function create(array $data)
    {
        $answer = ExaminationTypeModels::create($data, ['title','type']);
        return $answer->isEmpty()?false:$answer->id;
    }

    /**
     * 批量删除
     * @param mixed $data
     * @return boolean
     */
    public function destory( $data )
    {
        return ExaminationTypeModels::destroy(function($query)use($data){
            $where = ['id', '=', $data];
            if ( is_array($data) ) {
                $where = ['id', 'in', $data];
            }
            $query->where([
                $where
            ]);
        });
    }

    /**
     * 考试类型管理编辑
     * @param ExaminationTypeModels $model
     * @param array $data
     */
    public function save(ExaminationTypeModels $model, array $data)
    {
        $model->title = $data['title'];
        $model->type = $data['type'];
        return $model->save();
    }

    /**
     * 根据类型获取考试类型
     * @param int $type
     *
     */
    public function getType( int $type=1 )
    {
        return ExaminationTypeModels::type($type)->select();
    }

}